/**
 * 弹出层选择组织机构树
 * @param orgCode
 * @param has_bm
 * @param orgKind
 * @param ifRadio
 * @param callBackFunc
 * @param serial
 */
function selectOrgTree(orgCode, has_bm, orgKind, ifRadio, callBackFunc, serial) {
	new newselectOrg().selectTree(orgCode,has_bm,orgKind,ifRadio,callBackFunc,serial);
}


/**
 * 弹出层选择人员树
 * @param orgCode
 * @param orgKind
 * @param staffKind
 * @param ifRadio
 * @param callBackFunc
 * @param serial
 */
function selectStaffTree(orgCode, orgKind, staffKind, ifRadio, callBackFunc, serial) {
	new newselectStaff().selectStaffTree(orgCode, orgKind, staffKind, ifRadio, callBackFunc, serial);
}

/**
 * 弹出层选择当前部门人员树
 * @param orgId
 * @param orgKind
 * @param staffKind
 * @param ifRadio
 * @param callBackFunc
 * @param serial
 */
function selectSameOrgStaff(orgId, ifRadio, callBackFunc, serial) {
	new newSameOrgStaff(callBackFunc).sameOrgStaff(orgId, ifRadio, callBackFunc, serial);
}

function newselectOrg() {
	//orgTree是组织树
	this.orgTrees = null;
	//组织树设置
	this.orgSetting = {
		view: {
			selectedMulti: false
		},
		edit: {
			enable: false,
			editNameSelectAll: false,
		},
		data: {
			simpleData: {
				enable: true,
				idKey: "id",
				pIdKey: "pid"
			}
		},
		check: {
			chkStyle: "checkbox", //勾选框类型
			enable: true, //设置 zTree 的节点上是否显示 checkbox
			chkboxType: {
				"Y": '',
				"N": ''
			}
		},
		callback: {
			beforeClick: function(treeId, treeNode) {}
		},
		async: {
			enable: true,
			url: prc + "/gacxOrgController/disOrgTree",
			autoParam: ["id", "has_bm", "orgKind"],
			dataFilter: ajaxDataFilter //处理异步加载返回的数据
		}
	};
	//查看异步加载返回数据的childNodes是否有值,有值说明展示处室节点,无值说明不展示处室节点
	function ajaxDataFilter(treeId, parentNode, childNodes) {
		var dd = childNodes.length; //只要有一个childNOdes节点的id,就证明可以展示下一级
		if (dd == 0) {
			return null;
		} else {
			return childNodes;

		}
	}
	//加载组织树方法
	//orgCode:组织编码  如果orgCode为空,则传山东省的orgID作为根节点
	//has_bm:1 (默认)展示处室  0  不展示处室
	this.selectTree = function selectTree(orgId, has_bm, orgKind, ifRadio, callbackFunc, serial) {
		var oset = this.orgSetting;
		if (null == orgId || "" == orgId || undefined == orgId) {
			if ($('#orgTreeAlertModal').size() == 0) {
				$('body').prepend("<div class='modal fade' id='orgTreeAlertModal' tabindex='-1' role='dialog' aria-labelledby='orgTreeAlertModalLabel'>" +
						"<div class='modal-dialog' role='document'><div class='modal-content'>" +
						"<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'>" +
						"<span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'>提示</h4></div>" +
						"<div class='modal-body'><div class='form-group'><label id='tishi' for='txt_departmentname'>根节点不可为空</label>" +
						"</div></div>" +
						"<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>" +
						"<span class='glyphicon' aria-hidden='true'></span>关闭</button>" +
						"<button type='button' id='btn_submit' class='btn btn-primary' data-dismiss='modal'>" +
						"<span class='glyphicon' aria-hidden='true'></span>确定</button></div></div></div></div>");
			}
			$("#tishi").html("根节点不可为空");
			$('#orgTreeAlertModal').modal('show');
			return;
		}
		if (null == has_bm || "" == has_bm || undefined == has_bm) {
			has_bm = "1";
		}
		if (null == orgKind || "" == orgKind || undefined == orgKind) {
			orgKind = "1";
		}
		$.post(prc + '/gacxOrgController/selectorgTree.action', {
			'orgId':orgId,
			'has_bm': has_bm,
			'orgKind': orgKind
		}, (function(orgSetting) {
			return function(data) {
				if (ifRadio == "1" || null == ifRadio || "" == ifRadio) {
					orgSetting.check.enable = false;
					orgSetting.callback.beforeClick = function(treeId, treeNode) {
						var n = treeNode.id;
						var v = treeNode.name;
						callbackFunc(n, v);
						$('#orgTreeModal'+serial).modal('hide');
					};
				} else if (ifRadio == "2") {
					orgSetting.check.enable = true;
					orgSetting.check.chkStyle = "checkbox";
					orgSetting.check.chkboxType = {
						"Y": '',
						"N": ''
					};
				}
				if ($('#orgTreeModal'+serial).size() == 0) {
					var content = "<div class='modal fade' id='orgTreeModal"+serial+"' tabindex='-1' role='dialog' aria-labelledby='orgTreeModelLabel'><div class='modal-dialog' role='document'>" +
					"<div class='modal-content'>" +
					"<div class='modal-header'>" +
					"<button type='button' class='close' data-dismiss='modal' aria-label='Close'>" +
					"<span aria-hidden='true'>&times;" + "</span>" +
					"</button>" +
					"<h4 class='modal-title'>请选择组织树</h4>" +
					"</div>" + "<div class='modal-body'>" + "<ul id='orgTrees"+serial+"' class='ztree' style='width: 560px; overflow: auto;'></ul>" +
					"</div>" +
					"<div class='modal-footer'>" +
					"<button type='button' class='btn btn-primary' data-dismiss='modal' name='doSelectOrgButton' onclick='javascript:doSelectOrgTree("+serial+","+callbackFunc+");'>确定</button>" +
					"<button type='button' class='btn btn-default' data-dismiss='modal' name='orgTreeCloseModal'>关闭</button>" +
					"</div></div></div></div>";
					$('body').prepend(content);
					orgTrees = $.fn.zTree.init($("#orgTrees"+serial+""), orgSetting, data);
					orgTrees.expandAll(false);
				}
				$('#orgTreeModal'+serial).modal('show');
			}
		})(oset));
	}
}

//点击确定按钮的方法
function doSelectOrgTree(serial,callbackFunc) {
	var treeObj = $.fn.zTree.getZTreeObj("orgTrees"+serial);
	var nodes = treeObj.getCheckedNodes(true);
	if (nodes.length === 0) {
		callbackFunc('', '', '');
		return;
	}
	var ids = "";
	var names = "";
	for (var i = 0; i < nodes.length; i++) {
		ids += nodes[i].id + ",";
		names += nodes[i].name + ",";
	}
	callbackFunc(ids.substr(0, ids.length - 1), names.substr(0, names.length - 1));
}
//仅供测试使用的回调函数
function yourFunction(ids,names){
	console.log("ids:"+ids+";names:"+names);
	$("#org").val(names);
    $("#orgid1").val(ids);
    $("#orgName1").val(names);
}
function yourFunction2(ids,names){
    console.log("ids:"+ids+";names:"+names);
    $("#org2").val(names);
    $("#orgid2").val(ids);
    $("#orgName2").val(names);
}
function yourFunction3(ids,names){
    console.log("ids:"+ids+";names:"+names);
    $("#org3").val(names);
    $("#orgid3").val(ids);
    $("#orgName3").val(names);
}
function yourFunction4(ids,names){
    console.log("ids:"+ids+";names:"+names);
    $("#org4").val(names);
    $("#orgid4").val(ids);
    $("#orgName4").val(names);
}
//****************************************************************************
function newSameOrgStaff(funName){
	//引用的回调函数
	this.callBackSfunName = funName;
	//staffTree是人员选择树
	this.staffTrees = null;
	//人员选择树设置
	this.staffSetting = {
		view: {
			selectedMulti: false
		},
		edit: {
			enable: false,
			editNameSelectAll: false,
		},
		data: {
			simpleData: {
				enable: true,
				idKey: "orgId",
				pIdKey: "pid"
			}
		},
		check: {
			chkStyle: "checkbox", //勾选框类型
			enable: true, //设置 zTree 的节点上是否显示 checkbox
			chkboxType: {
				"Y": '',
				"N": ''
			}
		},
		callback: {
			beforeClick: function(treeId, treeNode) {},
			onCheck:function(e, treeId, treeNode) {}
		}
	};
	//加载人员选择方法
	//orgCode:组织编码
	//orgKind:'1' 甲方 '2' 乙方
	//staffKind:'1' 甲方 '2' 乙方
	//ifRadio:'1'单选'2'多选
	this.sameOrgStaff = function sameOrgStaff(orgId, ifRadio, callBackFunc, serial) {
		var sset = this.staffSetting;
		if (null == orgId || "" == orgId || undefined == orgId) {
			if ($('#staffTreeAlertModal').size() == 0) {
				$('body').append("<div class='modal fade' id='staffTreeAlertModal' tabindex='-1' role='dialog' aria-labelledby='orgTreeAlertModalLabel'>" +
						"<div class='modal-dialog' role='document'><div class='modal-content'>" +
						"<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'>" +
						"<span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'>提示</h4></div>" +
						"<div class='modal-body'><div class='form-group'><label id='tishi' for='txt_departmentname'>根节点不可为空</label>" +
						"</div></div>" +
						"<div class='modal-footer'><button type='button' id='btn_submit' class='btn btn-primary' data-dismiss='modal'>" +
						"<span class='glyphicon' aria-hidden='true'></span>确定</button><button type='button' class='btn btn-default' data-dismiss='modal'>" +
						"<span class='glyphicon' aria-hidden='true'></span>关闭</button>" +
						"</div></div></div></div>");
			}
			$("#tishi").html("根节点不可为空");
			$('#staffTreeAlertModal').modal('show');
			return;
		}
		$.post(prc + '/gacxOrgController/loadSameOrgStaff.action', {
			'orgId': orgId
		}, (function(staffSetting) {
			return function(data) {
				if (ifRadio == "1" || null == ifRadio || "" == ifRadio) {
					//staffSetting.check.chkStyle="radio";
					staffSetting.check.enable = false;
					staffSetting.callback.beforeClick = function(treeId, treeNode) {
						var treeObj=$.fn.zTree.getZTreeObj("staffTrees"+serial);
						if (treeNode.isParent) {
							if ($('#staffModal'+serial).size() !=0) {
								$("#jinggao").html("组织节点不可选！");
							}
							$("#jinggao").html("");
							treeObj.cancelSelectedNode(treeNode);
							treeObj.checkNode(treeNode);
							return;
						}
						orgId = treeNode.orgId;
						name = treeNode.name;
						soid = treeNode.staffOid;
						sid = treeNode.staffId;
						callBackFunc(orgId,name,soid,sid);
						$('#staffModal'+serial).modal('hide');
					};
				} else if (ifRadio == "2") {
					staffSetting.check.enable = true;
					staffSetting.check.chkStyle = "checkbox";
					staffSetting.check.chkboxType = {
						"Y": '',
						"N": ''
					};
					staffSetting.callback.onCheck = function(e, treeId, treeNode) {
						var treeObj = $.fn.zTree.getZTreeObj("staffTrees"+serial);
						nodes = treeObj.getCheckedNodes(true);
						
						for (var i = 0; i < nodes.length; i++) {
							if (nodes[i].isParent) {
								if($('#staffModal'+serial).size() !=0){
									$("#jinggao").html("组织节点不可选！");
								}
								treeObj.cancelSelectedNode(nodes[i]);
								treeObj.checkNode(nodes[i]);
								return;
							}
							$("#jinggao").html("");
							var orgId="";
							var name ="";
							var soid ="";
							var sid = "";
							orgId += nodes[i].orgId+",";
							name += nodes[i].name + ",";
							soid += nodes[i].staffOid + ",";
							sid += nodes[i].staffId + ",";
						}
						callBackFunc(orgId,name,soid,sid);
						//$('#staffModal'+serial).modal('hide');
					};
				}
				if ($('#staffModal'+serial).size() == 0) {
					var content = "<div class='modal fade' id='staffModal"+serial+"' tabindex='-1' role='dialog' aria-labelledby='disTreeModelLabel'><div class='modal-dialog' role='document'>" +
					"<div class='modal-content'>" +
					"<div class='modal-header'>" +
					"<button type='button' class='close' data-dismiss='modal' aria-label='Close'>" +
					"<span aria-hidden='true'>&times;" + "</span>" +
					"</button>" +
					"<h4 class='modal-title'>请选择人员选择树</h4>" +
					"<span id='jinggao' style='color:red;'></span>"+
					"</div>" + "<div class='modal-body'>" + "<ul id='staffTrees"+serial+"' class='ztree' style='width: 560px; overflow: auto;'></ul>" +
					"</div>" +
					"<div class='modal-footer'>" +
					"<button type='button' class='btn btn-primary' data-dismiss='modal' name='doSelectstaffButton' onclick='javascript:doSelectStaffTree("+serial+","+callBackFunc+");'>确定</button>" +
					"<button type='button' class='btn btn-default' data-dismiss='modal' name='staffTreeCloseModal'>关闭</button>" +
					"</div></div></div></div>";
					$('body').prepend(content);
					staffTrees = $.fn.zTree.init($("#staffTrees"+serial+""), staffSetting, data);
					staffTrees.expandAll(true);
				}
				$('#staffModal'+serial).modal('show');
			}
		})(sset));
	}
}

//*****************************************************************************

function newselectStaff(funName) {
	//引用的回调函数
	this.callBackSfunName = funName;
	//staffTree是人员选择树
	this.staffTrees = null;
	//人员选择树设置
	this.staffSetting = {
		view: {
			selectedMulti: false
		},
		edit: {
			enable: false,
			editNameSelectAll: false,
		},
		data: {
			simpleData: {
				enable: true,
				idKey: "orgId",
				pIdKey: "pid"
			}
		},
		check: {
			chkStyle: "checkbox", //勾选框类型
			enable: true, //设置 zTree 的节点上是否显示 checkbox
			chkboxType: {
				"Y": '',
				"N": ''
			}
		},
		callback: {
			beforeClick: function(treeId, treeNode) {},
			onCheck:function(e, treeId, treeNode) {}
		},
		async: {
			enable: true,
			url: prc + "/gacxOrgController/disStaffTree",
			autoParam: ["orgId", "orgKind", "staffKind"],
			dataFilter: ajaxDataFilter2 //处理异步加载返回的数据
		}
	};

//查看异步加载返回数据的childNodes是否有值,有值说明展示处室节点,无值说明不展示处室节点
function ajaxDataFilter2(treeId, parentNode, childNodes) {
		var dd = childNodes.length; //只要有一个childNOdes节点的id,就证明可以展示下一级
		if (dd == 0) {
			return null;
		} else {
			return childNodes;

		}
	}
	//加载人员选择方法
	//orgCode:组织编码
	//orgKind:'1' 甲方 '2' 乙方
	//staffKind:'1' 甲方 '2' 乙方
	//ifRadio:'1'单选'2'多选
	this.selectStaffTree = function selectStaffTree(orgId, orgKind, staffKind, ifRadio, callBackFunc, serial) {
		var sset = this.staffSetting;
		if (null == orgId || "" == orgId || undefined == orgId) {
			if ($('#staffTreeAlertModal').size() == 0) {
				$('body').append("<div class='modal fade' id='staffTreeAlertModal' tabindex='-1' role='dialog' aria-labelledby='orgTreeAlertModalLabel'>" +
						"<div class='modal-dialog' role='document'><div class='modal-content'>" +
						"<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'>" +
						"<span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'>提示</h4></div>" +
						"<div class='modal-body'><div class='form-group'><label id='tishi' for='txt_departmentname'>根节点不可为空</label>" +
						"</div></div>" +
						"<div class='modal-footer'><button type='button' id='btn_submit' class='btn btn-primary' data-dismiss='modal'>" +
						"<span class='glyphicon' aria-hidden='true'></span>确定</button><button type='button' class='btn btn-default' data-dismiss='modal'>" +
						"<span class='glyphicon' aria-hidden='true'></span>关闭</button>" +
						"</div></div></div></div>");
			}
			$("#tishi").html("根节点不可为空");
			$('#staffTreeAlertModal').modal('show');
			return;
		}
		if (null == orgKind || "" == orgKind || undefined == orgKind) {
			orgKind = "1";
		}
		if (null == staffKind || "" == staffKind || undefined == staffKind) {
			staffKind = "1";
		}
		$.post(prc + '/gacxOrgController/selectStaffTree.action', {
			'orgId': orgId,
			'orgKind': orgKind,
			'staffKind': staffKind
		}, (function(staffSetting) {
			return function(data) {
				if (ifRadio == "1" || null == ifRadio || "" == ifRadio) {
					//staffSetting.check.chkStyle="radio";
					staffSetting.check.enable = false;
					staffSetting.callback.beforeClick = function(treeId, treeNode) {
						var treeObj=$.fn.zTree.getZTreeObj("staffTrees"+serial);
						if (treeNode.isParent) {
							if ($('#staffModal'+serial).size() !=0) {
									$("#jinggao").html("组织节点不可选！");
								
								/*$('body').append("<div class='modal fade' id='staffTreeAlertModal' tabindex='-1' role='dialog' aria-labelledby='orgTreeAlertModalLabel'>" +
										"<div class='modal-dialog' role='document'><div class='modal-content'>" +
										"<div class='modal-header'><button type='button' class='close' data-dismiss='modal' aria-label='Close'>" +
										"<span aria-hidden='true'>×</span></button><h4 class='modal-title' id='myModalLabel'>提示</h4></div>" +
										"<div class='modal-body'><div class='form-group'><label id='tishi' for='txt_departmentname'>根节点不可为空</label>" +
										"</div></div>" +
										"<div class='modal-footer'><button type='button' class='btn btn-default' data-dismiss='modal'>" +
										"<span class='glyphicon' aria-hidden='true'></span>关闭</button>" +
										"<button type='button' id='btn_submit' class='btn btn-primary' data-dismiss='modal'>" +
										"<span class='glyphicon' aria-hidden='true'></span>确定</button></div></div></div></div>");*/
							}
							//$("#tishi").html("根节点不可为空");
						//	$('#staffTreeAlertModal').modal('show');
							$("#jinggao").html("");
							treeObj.cancelSelectedNode(treeNode);
							treeObj.checkNode(treeNode);
							return;
						}
						orgId = treeNode.orgId;
						name = treeNode.name;
						soid = treeNode.staffOid;
						sid = treeNode.staffId;
						callBackFunc(orgId,name,soid,sid);
						$('#staffModal'+serial).modal('hide');
					};
				} else if (ifRadio == "2") {
					staffSetting.check.enable = true;
					staffSetting.check.chkStyle = "checkbox";
					staffSetting.check.chkboxType = {
						"Y": '',
						"N": ''
					};
					staffSetting.callback.onCheck = function(e, treeId, treeNode) {
						var treeObj = $.fn.zTree.getZTreeObj("staffTrees"+serial);
						nodes = treeObj.getCheckedNodes(true);
						
						for (var i = 0; i < nodes.length; i++) {
							if (nodes[i].isParent) {
								if($('#staffModal'+serial).size() !=0){
									$("#jinggao").html("组织节点不可选！");
								}
								treeObj.cancelSelectedNode(nodes[i]);
								treeObj.checkNode(nodes[i]);
								return;
							}
							$("#jinggao").html("");
							var orgId="";
							var name ="";
							var soid ="";
							var sid = "";
							orgId += nodes[i].orgId+",";
							name += nodes[i].name + ",";
							soid += nodes[i].staffOid + ",";
							sid += nodes[i].staffId + ",";
						}
						callBackFunc(orgId,name,soid,sid);
						//$('#staffModal'+serial).modal('hide');
					};
				}
				if ($('#staffModal'+serial).size() == 0) {
					var content = "<div class='modal fade' id='staffModal"+serial+"' tabindex='-1' role='dialog' aria-labelledby='disTreeModelLabel'><div class='modal-dialog' role='document'>" +
					"<div class='modal-content'>" +
					"<div class='modal-header'>" +
					"<button type='button' class='close' data-dismiss='modal' aria-label='Close'>" +
					"<span aria-hidden='true'>&times;" + "</span>" +
					"</button>" +
					"<h4 class='modal-title'>请选择人员选择树</h4>" +
					"<span id='jinggao' style='color:red;'></span>"+
					"</div>" + "<div class='modal-body'>" + "<ul id='staffTrees"+serial+"' class='ztree' style='width: 560px; overflow: auto;'></ul>" +
					"</div>" +
					"<div class='modal-footer'>" +
					"<button type='button' class='btn btn-primary' data-dismiss='modal' name='doSelectstaffButton' onclick='javascript:doSelectStaffTree("+serial+","+callBackFunc+");'>确定</button>" +
					"<button type='button' class='btn btn-default' data-dismiss='modal' name='staffTreeCloseModal'>关闭</button>" +
					"</div></div></div></div>";
					$('body').prepend(content);
					staffTrees = $.fn.zTree.init($("#staffTrees"+serial+""), staffSetting, data);
					staffTrees.expandAll(false);
				}
				$('#staffModal'+serial).modal('show');
			}
		})(sset));
	}
}
//点击确定按钮的方法
function doSelectStaffTree(serial,callbackFunc) {
	var treeObj = $.fn.zTree.getZTreeObj("staffTrees"+serial);
	var nodes = treeObj.getCheckedNodes(true);
	if (nodes.length === 0) {
		callbackFunc('', '', '');
		return;
	}
	var ids = "";
	var names = "";
	var soid = "";
 	var sid = "";
	for (var i = 0; i < nodes.length; i++) {
		ids= nodes[i].orgId+",";	
     	names+=nodes[i].name + ",";
     	soid+=nodes[i].staffOid + ",";
     	sid+=nodes[i].staffId+",";
	}
	callbackFunc(ids.substr(0, ids.length - 1), names.substr(0, names.length - 1), soid.substr(0, soid.length - 1),sid.substr(0, sid.length - 1));
}
//测试用回调函数
function staffFunc1(ids,names,soid,sid){
    console.log("ids:"+ids+";names:"+names+";soid"+soid+";sid"+sid+";");
    $("#ostaff1").val(names);
    $("#staffid1").val(sid);
    $("#staffName1").val(names);
    $("#staffOid1").val(soid);
    $("#orgid1").val(ids);
}
function staffFunc2(ids,names,soid,sid){
    console.log("ids:"+ids+";names:"+names+";soid"+soid+";sid"+sid+";");
    $("#ostaff2").val(names);
    $("#staffid2").val(sid);
    $("#staffName2").val(names);
    $("#staffOid2").val(soid);
    $("#orgid2").val(ids);
}


